package com.diyizu.controller.impl;

import com.diyizu.domain.Student;
import com.diyizu.excle.ExportExcel;
import com.diyizu.factory.Factory;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import java.io.File;
import java.util.ArrayList;
import java.util.Scanner;

public class ToExcle {
    Scanner sc = new Scanner(System.in);
    public String getUrl(){
        System.out.println("请输入导出路径：");
        String url = sc.next();
        File file = new File(url);
        file.mkdirs();
        return file.getPath();
    }
    public void toExcel() {
        long start = System.currentTimeMillis();
        HSSFWorkbook weebBook = new HSSFWorkbook();
        ArrayList<Student> students = Factory.getStudentInfoDao().findAllStudents();
        String[] arr = {"","学号","姓名","性别", "年龄", "年级", "班级", "小组", "手机号", "学生地址"};
        for (int i = 0; i < 2; i++) {
            HSSFSheet sheet = weebBook.createSheet("页签" + i);
            ArrayList<Object[]> strings = new ArrayList<>();
            for (int i1 = 0; i1 < students.size(); i1++) {
                                                                                                                                                                                                        //学号(唯一)  姓名 性别 年龄 年级 班级 小组 手机号 学生地址
                strings.add(new String[]{String.valueOf(i), students.get(i1).getSid(), students.get(i1).getName(), students.get(i1).getSex(), String.valueOf(students.get(i1).getAge()), String.valueOf(students.get(i1).getGrade()), students.get(i1).getClasses(), String.valueOf(students.get(i1).getGroup()), students.get(i1).getPhone(), String.valueOf(students.get(i1).getAddress())});
            }
            ExportExcel excel = new ExportExcel("201班信息表", arr, strings);
            try {
                excel.export(weebBook, sheet, i);
            } catch (Exception e) {
                System.out.println("文件路径错误!");
            }
        }
    }

}
